//
// Created by aaa on 2025/7/13.
//

#include<bits/stdc++.h>

using namespace std;

const int N = 6e3 + 10;

int n;
int w[N],b[N],fa[N],dp[N][2];
//动态数组存储
vector<int> a[N];

void dfs(int u){
    dp[u][1] = w[u];

    for(int i = 0;i < b[u];i++){
        int son = a[u][i];
        dfs(son);

        dp[u][0] += max(dp[son][0],dp[son][1]);
        dp[u][1] += dp[son][0];
    }
}

// 没有上司的舞会 P1352
//7
//1
//1
//1
//1
//1
//1
//1
//1 3
//2 3
//6 4
//7 4
//4 5
//3 5
int main(){
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> w[i];

    for(int i = 0;i < n - 1;i++){
        int x,y;
        cin >> x >> y;

        a[y].push_back(x);
        b[y]++;
        fa[x] = 1;
    }

    int root = 1;
    while(fa[root]) root++;
    dfs(root);
    cout << max(dp[root][0],dp[root][1]);

    return 0;
}